package servlet;

import controller.ControllerAnnunciLocal;
import controller.ControllerFeedbackLocal;
import entity.Adesione;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utilities.Utilities;

/**
 * Servlet per le operazioni di feedback verso creatori di annunci e iscritti.
 * @author Ale
 */
public class FeedbackServlet extends HttpServlet {
    @EJB
    private ControllerFeedbackLocal controllerFeedback;
    @EJB
    private ControllerAnnunciLocal controllerAnnunci;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");

        if (action == null) {
            Utilities.errore("Nessuna action specificata alla FeedbackServlet", request, response);
        } else if (action.equals("mostraFeedbackPopup")) { //richiesta ajax
            mostraFeedbackPopup(request, response);
        } else if (action.equals("lasciaFeedbackCreatore")) {
            lasciaFeedbackCreatore(request, response);
        } else if (action.equals("lasciaFeedbackIscritto")) {
            lasciaFeedbackIscritto(request, response);
        }
    }

    private void mostraFeedbackPopup(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long idAdesione = Long.parseLong(request.getParameter("idAdesione"));
        String perChi = request.getParameter("perChi");
        Adesione adesione = controllerAnnunci.getAdesione(idAdesione);
        request.setAttribute("adesione", adesione);
        request.setAttribute("perChi", perChi);
        Utilities.forward("/WEB-INF/jspf/lasciaFeedback.jsp", request, response);
    }

    private void lasciaFeedbackCreatore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long idAdesione = Long.parseLong(request.getParameter("idAdesione"));
        Adesione adesione = controllerAnnunci.getAdesione(idAdesione);
        int star = Integer.parseInt(request.getParameter("starFeedback"));
        controllerFeedback.lasciaFeedbackCreatore(adesione, star);
        Utilities.mostraAnnunciIscritto(request, response, adesione);
    }

    private void lasciaFeedbackIscritto(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long idAdesione = Long.parseLong(request.getParameter("idAdesione"));
        Adesione adesione = controllerAnnunci.getAdesione(idAdesione);
        int star = Integer.parseInt(request.getParameter("starFeedback"));
        controllerFeedback.lasciaFeedbackIscritto(adesione, star);
        Utilities.mostraAnnunciCreati(request, response, adesione.getAnnuncio());
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
}
